﻿using System;

namespace ScrappyDB.Utilities
{
    /// <summary>
    /// Source: http://stackoverflow.com/questions/298976/c-is-there-a-better-alternative-than-this-to-switch-on-type
    /// </summary>
    public static class TypeSwitch
    {
        public static void Do(object source, params CaseInfo[] cases)
        {
            Type type = source.GetType();
            foreach (CaseInfo entry in cases)
            {
                if (entry.IsDefault || entry.Target.IsAssignableFrom(type))
                {
                    entry.Action(source);

                    break;
                }
            }
        }

        public static CaseInfo Case<T>(Action action)
        {
            return new CaseInfo {Action = x => action(), Target = typeof (T)};
        }

        public static CaseInfo Case<T>(Action<T> action)
        {
            return new CaseInfo {Action = (x) => action((T) x), Target = typeof (T)};
        }

        public static CaseInfo Default(Action action)
        {
            return new CaseInfo {Action = x => action(), IsDefault = true};
        }

        #region Nested type: CaseInfo

        public class CaseInfo
        {
            public bool IsDefault { get; set; }
            public Type Target { get; set; }
            public Action<object> Action { get; set; }
        }

        #endregion
    }
}